<?php

/**
 * 文章管理
 * @author 陈德本
 *
 */
class newsService extends commonService
{

    /**
     *
     * @var newsDao
     */
    protected $pao;

    public function __construct()
    {
        parent::__construct();
        $this->pao = InitPHP::getDao("news");
    }

    /**
     * 获取对应分类的数据
     * 
     * @param number $pid            
     * @return Ambigous <multitype:, boolean, multitype:multitype: >
     */
    public function getNews($pid = 1, $num = 6)
    {
        return $this->pao->getAllByMutiple(array(
            'pid' => $pid
        ), array(), array('id'=>'desc'), $num);
    }

    public function incViews($id, $views)
    {
        $data['views'] = $views + 1;
        $this->pao->update($id, $data);
    }
    public function getPrevAndNext($id){
        $data=$this->pao->getOne($id);
        $sql="select * from news where id>$id and pid=".$data['pid']." order by id asc";
        $result['next']=$this->pao->queryRow($sql);
        $sql="select * from news where id<$id and pid=".$data['pid']." order by id desc";
        $result['prev']=$this->pao->queryRow($sql);
        return $result;
    }

    public function getList($pid, $start, $pernum)
    {
        $field = array(
            'pid' => $pid
        );
        $result = $this->pao->getList($field, $pernum, $start,'id','desc');
        $count = $this->pao->count($field);
        return array(
            $result,
            $count
        );
    }

    /**
     * 获取产品分类
     *
     * @param string $tree
     *            是否返回Tree
     * @return Ambigous <multitype:, multitype:array , boolean, multitype:multitype: >
     */
    public function getMenu($tree = TRUE)
    {
        $data = $this->pro_cat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'
        ));
        if ($tree) {
            $data = list_to_tree($data, "id", "pid", "products");
        }
        return $data;
    }
}